package com.more.spring.boot.springbootredisdemo.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Service
public class RedisService {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 写入和读取数据
     * redisTemplate.opsForValue().set(key, value);
     * @param key
     * @param value
     * @return
     */
    public boolean set(final String key, Object value, long expireTime){
        boolean result = false;
        try{
            ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
            operations.set(key, value);

            //设置失效事件
            redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
            result = true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 添加和获取散列表数据
     * redisTemplate.opsForHash().put(key, hashKey, value)
     * @param key
     * @param hashKey
     * @param value
     */
    public void hmSet(String key, Object hashKey, Object value){
        HashOperations<String, Object, Object> hashOperations = redisTemplate.opsForHash();
        hashOperations.put(key, hashKey, value);
    }

    /**
     * 添加列表数据
     * redisTemplate.opsForList().rightPush(key, value);
     * @param key
     * @param value
     */
    public void lpush(String key, Object value){
        ListOperations<String, Object> listOperations = redisTemplate.opsForList();
        listOperations.rightPush(key, value);
    }

    /**
     * redisTemplate.opsForList().range(key, from, to) 获取散列表
     * @param key
     * @param from
     * @param to
     * @return
     */
    public List<Object> lRange(String key, long from, long to){
        ListOperations<String, Object> listOperations = redisTemplate.opsForList();
        return listOperations.range(key, from, to);
    }

    /**
     * redisTemplate.opsForSet.add(key, value);添加set类型数据
     * @param key
     * @param value
     */
    public void add(String key, Object value){
        SetOperations<String, Object> setOperations = redisTemplate.opsForSet();
        setOperations.add(key, value);
    }
    /**
     * redisTemplate.opsForSet().members(key)活动redis的集合数据
     * @param key
     * @return
     */
    public Set<Object> setMembers(String key){
        SetOperations<String, Object> setOperations = redisTemplate.opsForSet();
        return setOperations.members(key);
    }

    /**
     * 添加有序集合数据
     * redisTemplate.opsForZSet().add(key, value, scoure)
     * @param key
     * @param value
     * @param score
     */
    public void zAdd(String key, Object value, double score){
        ZSetOperations<String, Object> zSetOperations = redisTemplate.opsForZSet();
        zSetOperations.add(key, value, score);
    }

    /**
     * 获取有序集合
     * @param key
     * @param scoure
     * @param score1
     * @return
     */
    public Set<Object> rangeByScore(String key, double scoure, double score1){
        ZSetOperations<String, Object> zSetOperations = redisTemplate.opsForZSet();
        return zSetOperations.rangeByScore(key, scoure, score1);
    }
}
